/**
 * @name        jQuery FullScreen Plugin
 * @author      Martin Angelov, Morten Sjøgren, kevin won
 * @version     1.3
 * @url         https://github.com/kevinwon1985/jQuery-FullScreen
 * @license     MIT License
 */

/*jshint browser: true, jquery: true */
(function($){
	"use strict";

	// These helper functions available only to our plugin scope.
	function supportFullScreen(){
		return document.fullScreenEnabled ||
				document.mozFullScreenEnabled ||
				document.webkitFullscreenEnabled ||
				document.msFullscreenEnabled;
	}

	function requestFullScreen(elem){
		if (elem.requestFullscreen) {
			elem.requestFullscreen();
		} else if (elem.mozRequestFullScreen) {
			elem.mozRequestFullScreen();
		} else if (elem.webkitRequestFullScreen) {
			elem.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
		} else if (elem.msRequestFullscreen) {
			elem.msRequestFullscreen();
		}
	}

	function fullScreenStatus(){
		isFullscreen = true;
		return document.fullscreen ||
				document.mozFullScreen ||
				document.webkitIsFullScreen ||
				document.msFullscreenElement != null ||
				false;
	}

	function cancelFullScreen(){
		isFullscreen = false;
		if (document.exitFullscreen) {
			document.exitFullscreen();
		} else if (document.mozCancelFullScreen) {
			document.mozCancelFullScreen();
		} else if (document.webkitCancelFullScreen) {
			document.webkitCancelFullScreen();
		} else if (document.msCancelFullScreen) {
			document.msCancelFullScreen();
		} else if(document.msExitFullscreen){
			document.msExitFullscreen();
		}
	}

	function onFullScreenEvent(callback){
		$(document).on("fullscreenchange mozfullscreenchange webkitfullscreenchange msfullscreenchange", function(){
			// The full screen status is automatically
			// passed to our callback as an argument.
			callback(fullScreenStatus());
		});
	}

	// Adding a new test to the jQuery support object
	$.support.fullscreen = supportFullScreen();

	var defaultOptions = {
		'fullscreenClass' : 'fullScreen'
	};
	// Creating the plugin
	$.fn.fullScreen = function(){
		// if (!$.support.fullscreen) {
		// 	alert("当前浏览器不支持自定义全屏, 可以使用系统的F11功能代替. 如要使用该功能, 请更换浏览器. 支持的浏览器有IE11, chrome, firefox.");
		// 	return this;
		// };

		if(this.length !== 1) {
			return this;
		}

		if(fullScreenStatus()){
			// if we are already in fullscreen, exit
			this.cancelFullScreen();
			return this;
		}

		// Inserting our element in the temporary
		// div, after which we zoom it in fullscreen

		requestFullScreen(this[0]);

		
		return this;
	};

	$.fn.cancelFullScreen = function( ) {
		cancelFullScreen();
		return this;
	};

	$.isFullScreen = fullScreenStatus;
}(jQuery));